package cn.pink.gamesrv.human;

import cn.pink.core.CallPoint;
import cn.pink.core.Port;
import cn.pink.core.Service;
import cn.pink.core.gen.proxy.DistrClass;
import cn.pink.core.gen.proxy.DistrMethod;
import cn.pink.core.support.Log;
import cn.pink.core.support.Param;
import lombok.Getter;

/**
 * 玩家service
 * 1个玩家对应一个HumanObjectService
 * @Author: pink
 * @Date: 2022/7/6 16:17
 */
@DistrClass(singleton = false, importClass = {CallPoint.class, Param.class})
public class HumanObjectService extends Service {

    /** 玩家对象 */
    @Getter
    private HumanObject humanObj;

    public HumanObjectService(Port port) {
        super(port);
    }

    @Override
    public Object getId() {
        return humanObj.getId();
    }

    @DistrMethod
    public void defaultHandler(String sessionId, byte[] msgBuf) {

    }

    @DistrMethod
    public void connClosed(String sessionId) {
        String currSessionId = (String) humanObj.getSessionPoint().servId;
        if (!currSessionId.equals(sessionId)) {
            Log.game.warn("session not match, sessionId={}, currSessionId={}", sessionId, currSessionId);
            return;
        }

        Log.game.info("connClosed: sessionId={}, humanId={}", sessionId, humanObj.getId());

        HumanGlobal.unregister(getPort(), humanObj.getId());
    }

    /**
     * 更换sessionPoint
     */
    public void changeSessionPoint(CallPoint sessionPoint) {
        Log.game.info("changeSessionPoint: old={}, new={}", humanObj.getSessionPoint(), sessionPoint);
        humanObj.setSessionPoint(sessionPoint);
    }

    /**
     * 登陆
     */
    public void login(CallPoint sessionPoint, String humanId, String userIdentity, String token) {
        // 创建玩家对象
        humanObj = new HumanObject(humanId, sessionPoint, userIdentity, token, this);
    }
}
